<!-- 列表功能模板 -->
<template>
  <div class="listBody">
    <!-- 搜索过滤 -->
    <div class="search">
      <div class="floatR">
        <button class="butOne" @click="findData(1,20)"><img src="../assets/img/find.png" />查询</button>
        <button class="butTwo" @click="clearSearch"><img src="../assets/img/cleck.png" />清除</button>
      </div>
      <div class="floatR">
        <el-select
          v-model="findState"
          placeholder="请选择"
          @change="selectChange">
        <el-option
          v-for="item in stateSelects"
          :key="item.value"
          :label="item.title"
          :value="item.value"          
        ></el-option>
      </el-select>
      </div>
      <div class="floatR">
        <el-date-picker          
          v-model="findDateRange"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="~"
          start-placeholder="录单开始日期"
          end-placeholder="录单结束日期"
          @change="dateChange"
        ></el-date-picker>
      </div>
      <div class="floatR">
        <el-input type="text" v-model="findTxt" placeholder="请输入关键字查询"></el-input>
      </div>
    </div>
    <!-- 操作按钮 -->
    <div class="operate" v-show="buttonParam != null && buttonParam.length > 0">
      <button
        v-for="item in buttonParam"
        :class="item.name.includes('删除') ? 'butTwo' : 'butOne'"
        @click="toAction(item)"
      >
        <img src="../assets/img/create.png" v-if="item.name.includes('新增') || item.name.includes('创建')" />
        <img src="../assets/img/ProcessReport_S.png" v-else-if="item.name.includes('审核')" />
        <img src="../assets/img/forbid.png" v-else-if="item.name.includes('禁用')" />
        <img src="../assets/img/SystemManage_S.png" v-else-if="item.name.includes('权限')" />
        <img src="../assets/img/VerifyOrder_S.png" v-else-if="item.name.includes('重置')" />
        <img src="../assets/img/detele.png" v-else-if="item.name.includes('删除')" />
        <img src="../assets/img/ProductionReport_S.png" v-else-if="item.name.includes('打印') || item.name.includes('套打')" />
        <img src="../assets/img/ProcessOrder_S.png" v-else-if="item.name.includes('导') || item.name.includes('引')" />
        <img src="../assets/img/Material_S.png" v-else />
        {{item.name}}
      </button>
    </div>
    <!-- 列表数据 -->
    <div class="table" v-if="tableParam != null && tableParam.length > 0">
      <el-table 
      border
      use-virtual
      highlight-current-row
      :data="dataItems"
      ref="multipleTable"
      class="tableList"
      rowKey="id"
      selectable="true"
      @row-dblclick="rowDblclick"
      @select="handleSelect" 
      @selection-change="handleSelectionChange" 
      :header-cell-style="{ background:'#FFF' }"
      >
        <el-table-column type="selection" width="60" fixed></el-table-column>
        <el-table-column 
          v-for="item in tableParam" 
          style="text-align: center;" 
          :prop="item.paramName" 
          :label="item.name" min-width="60" 
          :width="item.width" 
          v-show="item.hidden" 
          :fixed="item.freeze == 1 ? true:false" 
          sortable>
          <template slot-scope="scope">
            <div v-if="item.type == 'image'" style="width: 100%; text-align: center;">
              <img style="height: 60px; width: auto;"
              :src="scope.row.sex != null && scope.row.sex == 2 ?
              require('../assets/img/girl.png') : require('../assets/img/boy.png')" />
            </div>
             <span v-else-if="scope.row[scope.column.property] != null && scope.row[scope.column.property].toString().length > 0">{{scope.row[scope.column.property]}}</span>
             <span v-else style="font-weight: bold; font-size: 18px;">-</span>
          </template>
        </el-table-column>
        <template slot="empty">
          <div class="notData">
            <img src="../assets/img/notData.png" />暂无数据
          </div>
        </template>
      </el-table>      
    </div>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination
        layout="total, prev, pager, next, sizes"        
        :page-size="pageParam.pageSize"
        :current-page="pageParam.pageIndex"
        :total="pageParam.pageCount"
        :page-sizes="[20,50,100,200,300]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <div class="floatBG" v-show="allowOpen"></div>
    <div class="baseEdit" v-show="allowOpen == true && openType == 'baseEdit'">
      <base-edit :menuData="editData" :theOrg="theOrg"></base-edit>
    </div>
    <div class="orderEdit" v-show="allowOpen == true && openType == 'orderEdit'">
      <order-edit :menuData="editData" :theOrg="theOrg"></order-edit>
    </div>
  </div>
</template>
<script>
import rightData from "./rightData.vue";
import baseEdit from "./baseEdit.vue";
import orderEdit from "./orderEdit.vue";
export default {
  data() {
    return {
      menuName: "",
      menuKey: "",      
      showFilter: false,
      selectType: '',
      dataType:'',
      openType: '',
      //搜索
      filter: '',
      findSearch: '',
      findTxt: '',
      stateSelects:[{value:'',title:'-- 请选择状态 --'},{value:'2',title:'已审核'}],
      findState: '',
      findDateRange: '',
      searchData: '',
      submitUrl: '',
      editUrl: '',
      selectedIds:'',
      //按钮
      buttonParam:[],
      selectLine: false,
      tableParam:[],
      dataItems:[],
      pageParam:{ //分页对象
        pageIndex:1,
        pageSize:20,
        pageNum:0,
        pageCount:0
      },
      //操作弹窗口
      orgId: 0,
      openType: 'baseEdit',
      editData:{},
      allowOpen: false
    };
  },
  components:{
    rightData,
    baseEdit,
    orderEdit
  },
  props:{
    menuData:{
      type:Object,
      required:true,
      default:{}
    },
    theOrg:{
      type:Number,
      required:true
    }
  },
  created(){
    this.showData(1,20);
  },
  watch: {
    menuData(data){      
      this.menuKey = this.menuData.key;
      this.menuName = this.menuData.name;
      this.orgId = this.theOrg;
      this.submitUrl = '';
      this.findTxt = '';
      this.findState = '';
      if(this.findDateRange != null && this.findDateRange.trim().length > 0)
        this.findDateRange = '';
      this.stateSelects = [{value:'',title:'-- 请选择状态 --'}];
      this.selectType = '';
      this.dataType = '';
      this.filter = '';
      this.findSearch = '';
      this.selectedIds = '';
      this.buttonParam = [];
      this.tableParam = [];
      this.editData = {};
      this.allowOpen = false;
      this.pageParam = {
        pageIndex:1,
        pageSize:20,
        pageNum:0,
        pageCount:0
      };
      this.editUrl = '';
      if(this.menuData.openType.includes('list')){
        this.showData(1,20);
      }
    }
  },
  methods: {
    //展示组件数据
    async showData(){
      if(this.menuData != null && this.menuData.url != null){        
        let toUrl = this.changeUrl(this.menuData.url);
        let { data: res } = await this.$http.get(toUrl);
        if (res.result) {
          this.selectType = res.obj.selectType;
          this.dataType = res.obj.dataType;
          this.openType = res.obj.openType;
          this.filter = res.obj.filter;
          this.findSearch = res.obj.findSearch;
          if(res.obj.stateSelects != null)
            this.stateSelects = res.obj.stateSelects;
          this.buttonParam = res.obj.buttonParam;
          this.tableParam = res.obj.tableParam;
          this.pageParam = res.obj.pagingParam;
          this.editUrl = res.obj.editUrl;
          this.submitUrl = res.obj.submitUrl;
          let index = this.pageParam.pageIndex != null ? this.pageParam.pageIndex : 1;
          let size = this.pageParam.pageSize != null ? this.pageParam.pageSize : 20;
          this.findData(index,size);
        }else{
          this.clewMsg(res.type,res.msg);
        }
      }
    },
    //搜索列表数据
    async findData(index,size){
      let theSize = size != null && size > 0 ? size : 20;
      let theIndex = index != null && index > 0 ? index : 1;
      let searchData = '{ pageIndex: '+theIndex+',pageSize: '+theSize+'}';
      let findSearch = '';
      if(this.findTxt != null && this.findTxt.trim().length > 0){
        findSearch = this.findSearch.replace("'%{0}%'",this.findTxt.trim())
      }
      var postData = {
        orgId: this.theOrg,
        menuKey: this.menuData.key,
        dataType: this.dataType,
        selectType: this.selectType,
        filter: this.filter,
        findSearch: findSearch,
        findState: this.findState,
        findDateRange: this.findDateRange,
        searchData: searchData
      };
      let postUrl = this.changeUrl(this.submitUrl);
      let { data: res } = await this.$http.post(postUrl, postData);
      if (res.result) {
        this.pageParam = {
          pageIndex: res.obj.pageIndex,
          pageSize: res.obj.pageSize,
          pageNum: res.obj.pageNum,
          pageCount: res.obj.pageCount
        };
        this.selectedIds = '';
        this.dataItems = res.obj.items;
      } else {
        this.clewMsg(res.type,res.msg);
      }
    },
    //执行异步按钮
    async actionData(data){
      var butName = data.name;
      let selVal = this.selectedIds;
      if(selVal != null && selVal.trim().length > 0) {
        this.$confirm("将执行#"+butName+"#，是否确认继续？", "温馨提示", {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warnig"
        }).then(async () => {
          var toUrl = data.actionUrl + '&' + data.actionValue + '=' + selVal;
          toUrl = this.changeUrl(toUrl);
          let { data: res } = await this.$http.get(toUrl);
          if (res.result) {
            this.clewMsg(1,res.msg);
          }else{
            this.clewMsg(res.type,res.msg);
          }
          let index = this.pageParam.pageIndex != null ? this.pageParam.pageIndex : 1;
          let size = this.pageParam.pageSize != null ? this.pageParam.pageSize : 20;
          this.findData(index,size);
        });        
      }else{
        this.clewMsg(2,'请选择您需要#'+butName+'#操作的数据行');
      }
    },   
    toAction(data){
      if(data != null && data.actionType != null){
        let type = data.actionType;
        if(type === 'action'){
          this.actionData(data);
        }else if(type === 'edit'){
          this.openEdit(0);
        }else if(type === 'export'){
          this.toExport(data);
        }else if(type === 'import'){
          this.toImport(data);
        }else if(type === 'print'){
          this.toPrint(data);
        }
      }
    },
    //打开编辑窗口
    openEdit(id){
      let theId = id != null && id > 0 ? id : 0;
      var showUrl = this.editUrl + '&id=' + theId;
      this.editData = this.menuData;
      this.editData.showUrl = showUrl;
      this.editData.dataId = id;
      this.editData.openType = this.openType;
      this.allowOpen = true;
    },
    closeEdit(){
      this.allowOpen = false;
      this.editData = {};
      let index = this.pageParam.pageIndex != null ? this.pageParam.pageIndex : 1;
      let size = this.pageParam.pageSize != null ? this.pageParam.pageSize : 20;
      this.findData(index,size);
    },
    //导出数据（Excel文档）
    toExport(data){

    },
    //导入数据（打开模板下载）
    toImport(data){

    },
    //执行打印
    toPrint(data){

    },
    //刷新列表查询
    updateListData(){
      let index = this.pageParam.pageIndex != null ? this.pageParam.pageIndex : 1;
      let size = this.pageParam.pageSize != null ? this.pageParam.pageSize : 20;
      this.findData(index,size);
    },
    //勾选的时候来判断 让其变成单选
    handleSelect(val,row){
      if (val.length > 1) {
        this.$refs.multipleTable.clearSelection() // 清空所有选择
        val.shift()
        this.$refs.multipleTable.toggleRowSelection(row) //  选中当前选择
      }
      let selected = val.length && val.indexOf(row) !== -1
      //console.log(selected)  // true就是选中，0或者false是取消选中
    },
    //行选择
    handleSelectionChange(val){
      let posy = val;
      let pos = [];
      this.selectedIds = '';
      posy.forEach(item=>{
        pos.push(item.id);
        this.selectedIds = pos.join(',')//把选中的id已字符串传递出去  例如：'1,3,4,5'
      })
      console.log('选择行ID：');
      console.log(this.selectedIds);
    },
    //双击行打开孔编辑窗口
    rowDblclick(row) {
      if(this.openType != null && this.openType.length > 0)
        this.openEdit(row.id);
    },
    //分页切换分页数量
    handleSizeChange(val){
      this.pageParam.pageSize = val;
      let index = this.pageParam.pageIndex != null ? this.pageParam.pageIndex : 1;
      let size = this.pageParam.pageSize;
      this.findData(index,size);
    },
    //分页选择第几页
    handleCurrentChange(val){
      console.log('指定页码：》》');
      console.log(val);
      this.pageParam.pageIndex = val;
      let index = this.pageParam.pageIndex;
      let size = this.pageParam.pageSize != null ? this.pageParam.pageSize : 20;
      this.findData(index,size);
    },    
    //清空搜索组件内容
    clearSearch(){
      this.findTxt = '';
      this.findState = '';
      this.findDateRange = '';
    },
    selectChange(val){
      this.findState = val;
    },
    dateChange(val){
      if(val != null)
        this.findDateRange = val;
    },
    //处理URL自增加组织ID
    changeUrl(url){
      if(/\?|&/.test(url)){
        url += '&orgId=' + this.theOrg;
      }else{
        url += '?orgId=' + this.theOrg;
      }
      return url;
    },
    //提款信息
    clewMsg(type,mes){
      var theType = 'warning';
      if(type == 1)
        theType = 'success';
      else if(type == 3)
        theType = 'error';
      this.$message({
        dangerouslyUseHTMLString:true,
        message: mes,
        type: theType
      });
    }
  }
};
</script>
<style>
  .baseEdit{
    position: fixed;
    z-index: 555;
    top: 100px;
    left: calc((100% - 800px) / 2);
    width: 800px;
    max-height: 650px;
    overflow: hidden;
    background: #FFF;
    border-radius: 5px;
    box-shadow: 0 1px 2px 1px rgba(0,0,0,0.15);
  }
  .orderEdit{
    position: fixed;
    z-index: 666;
    top: 100px;
    left: calc((100% - 1300px) / 2);
    width: 1300px;
    max-height: 650px;
    overflow: hidden;
    background: #FFF;
    border-radius: 5px;
    box-shadow: 0 1px 2px 1px rgba(0,0,0,0.15);
  }
  .floatBG{
    position: fixed;
    z-index: 99;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.3);
  }
</style>
<style lang="less" scoped>
  .el-input{
    /deep/.el-input__inner{
      width: 260px;
      height: 34px;
      line-height: 34px;
      border: solid 1px #DDD;
      padding: 8px 10px;
      font-size: 15px;
      color: #222;
      border-radius: 4px;
    }
  }
  .floatR{
    /deep/.el-date-editor{
      width: 250px !important;
      height: 34px !important;
      border: solid 1px #DDD;
      padding: 5px 0 5px 10px;
      font-size: 15px;
      color: #222;
      border-radius: 4px;
      align-items: center !important;
    }
    /deep/.el-range__close-icon{
      width: 10px !important;
    }
    /deep/.el-range-input{
      width: 90px !important;
    }
  }
  .el-date-editor{
    /deep/.el-input__icon{
      line-height: 22px !important;
    }
    /deep/.el-range-separator{
      line-height: 22px !important;
    }
  }
  .el-select{    
    /deep/.el-input__inner {
      width: 160px !important;
      height: 34px !important;
      border: solid 1px #DDD;
      padding: 5px 10px;
      font-size: 15px;
      color: #222;
      border-radius: 4px;
    }
  }
  .table{
    clear: both;
    width: 100%;
    align-items: center;
    max-height: 460px;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 0;
    margin: 0;
    /deep/.el-table{
      width:100%;
      overflow: auto;
      max-height:calc(100% - 200px);
    }
    /deep/.el-table__header th{
      background: #EEE;
      font-weight:normal;
      color: #000;
    }
    /deep/.el-table__header .el-table__cell{
      padding: 5px 0;
    }
    /deep/.el-table__body-wrapper{
      width:100%;
    }
    /deep/tr .el-checkbox__input{
      margin-left: 10px;
    }
    /deep/td .el-checkbox__input{
      margin-left: 10px;
    }
    /deep/.el-checkbox__inner{
      width: 18px;
      height: 18px;
    }
    /deep/.el-checkbox__inner:hover{
      border-color: #00A578;
      box-shadow: 0 0 2px 1px rgba(42, 51, 49, 0.15);
    }
    /deep/.el-table thead{
      color: #FFF;
    }
    /deep/.el-table__empty-block{
      width: 0px;
    }
    /deep/.el-table__empty-text{
      text-align: left;
    }    
  }
  .paging {
    height: 40px;
    line-height: 30px;
    text-align: right;
    /deep/.el-pagination__total {
      line-height: 30px;
    }
    /deep/.el-pager {
      background: #FFFFFF;
      color:#00A578;
      border-radius: 4px;
      cursor: pointer !important;
      border: 1px solid #00A578;
      margin-right: 8px;
    }
    /deep/.el-pagination .btn-prev {
      background: #FFF;
      border: solid 1px #DDD;
      color: #999;
      border-radius: 4px;
      height: 32px;
      line-height: 30px;
      margin-right: 8px;
    }
    /deep/.el-pagination .btn-next {
      background: #FFF;
      border: solid 1px #DDD;
      color: #999;
      border-radius: 4px;
      height: 32px;
      line-height: 30px;
      margin-right: 3px;
    }
    /deep/.el-input__inner{
      border-radius: 4px;
      height: 32px;
      line-height: 30px;
    }
    /deep/.el-input__inner:hover{
      border: solid 1px #00A578;
      box-shadow: 0 0 3px 1px rgba(0,160,120,0.2);
    }
    /deep/.el-select-dropdown__item .selected{
      color: #000;
    }
    /deep/.number{
      color: #00A578;
    }
    /deep/.el-icon{
      font-size: 16px;
    }
  }
  .floatR > *:hover{
    box-shadow: 0 0 3px 1px rgba(0,160,120,0.2);
  }
   .select{
    width: 120px !important;
    height: 28px;
    line-height: 28px;
    border: solid 1px #DDD;
    padding: 5px 10px;
    font-size: 15px;
    color: #222;
    border-radius: 4px;
   }
</style>
<style>
  .listBody{
    width: 100%;
    height: 100%;
  }
  .floatR{
    height: 40px;
    float: right;
    margin-left: 8px;
  }
  /* 搜索 */
  .search{
    clear: both;
    width: 100% ;
    height: 40px;
    margin-bottom: 10px;
    text-align: right;
    align-items: center;
  }  

  /* 操作 */
  .operate{
    width: calc(100% - 20px);
    padding: 10px;
    justify-content: center;
    align-items: left;
    background-color: #FAFAFA;
    box-shadow: 0 0 1px 1px rgba(0,0,0,0.15);
    clear: both;
  }

 /* 列表 */  
  .notData{
    clear: both;
    height: 90px;
    padding: 10px 0;
    font-size: 15px;
    color: #BBB;
  }
  .notData img{
    height: 80px;
    width: auto;
    vertical-align:middle;
    margin-left: 200px;
  }

 /* 分页 */
 .paging{
    clear: both;
    height: 45px;
    width: 100%;
    padding: 10px 0;
    align-items: right;
  }
  /* 按钮 */
  .butOne{
    padding: 6px 12px;
    height: 34px;
    line-height: 24px;
    margin-right: 10px;
    font-size: 15px;
    color: #FFF;
    background: #00A578;
    border: 1px solid #00A578;
    border-radius: 4px;
    cursor: pointer;
    text-align: center;
  }
  .butTwo{
    padding: 6px 12px;
    height: 34px;
    line-height: 24px;
    margin-right: 10px;
    font-size: 15px;
    color: #000;
    background: #FFF;
    border: 1px solid #DDD;
    border-radius: 4px;
    cursor: pointer;
    text-align: center;
  }
  .butOne:hover{
    border-color: #DDD;
    box-shadow: 0 0 3px 1px rgba(0,0,0,0.15);
  }
  .butTwo:hover{
    border-color: #00A578;
    box-shadow: 0 0 3px 1px rgba(0,160,120,0.2);
  }
  .butOne img,.butTwo img{
    height: 14px;
    width: auto;
    margin-right: 5px;
  }

</style>
